//
//  JQFXHFeedbackViewController.swift
//  jiuzhoudai
//
//  Created by 卢小艳 on 2025/7/8.
//

import Foundation
import UIKit

class JQFXHFeedbackViewController: HJYJBaseViewController, UITextViewDelegate {
    
    // 定义问题类型的按钮选项

    
    lazy var jqfxhDesffLabel: UILabel = {
        let jqfxhDesffLabel = UILabel()
        jqfxhDesffLabel.text = "详细描述"
        jqfxhDesffLabel.font = UIFont.jqfxhMedium18
        jqfxhDesffLabel.textColor = .black
        return jqfxhDesffLabel
    }()
    

    
    
    lazy var jqfxhSubhhBtn: UIButton = {
        let jqfxhSubhhBtn = UIButton(type: .system)
        jqfxhSubhhBtn.backgroundColor = UIColor.jqfxhThemexxRed
        jqfxhSubhhBtn.setTitleColor(.white, for: .normal)
        jqfxhSubhhBtn.setTitle("提交", for: .normal)
        jqfxhSubhhBtn.layer.cornerRadius = 8
        jqfxhSubhhBtn.addTarget(self, action: #selector(jqfxhFeednnBackRequest), for: .touchUpInside)
        return jqfxhSubhhBtn
    }()
    
    lazy var jqfxhDesTextggView : JQFXHPlaceholderooTextView = {
        let jqfxhDesTextggView = JQFXHPlaceholderooTextView()
        jqfxhDesTextggView.jqfxhPlaceppholder = "请输入文本"
        jqfxhDesTextggView.textContainerInset = UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
        jqfxhDesTextggView.textColor = .black
        jqfxhDesTextggView.font = UIFont.systemFont(ofSize: 16)
        jqfxhDesTextggView.returnKeyType = .done
        jqfxhDesTextggView.layer.cornerRadius = 8
        jqfxhDesTextggView.jqfxhAddDoneButtoneeOnKeyboard(target: self, action: #selector(jqfxhDoneTapped))
        return jqfxhDesTextggView
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "意见反馈"
        self.view.backgroundColor = .jqfxhSmallViewBgbbGray
        
        jqfxhSetupiiUI()
    }
   @objc func jqfxhFeednnBackRequest() {
        if let text = jqfxhDesTextggView.text {
            if text.count == 0 {
                JQFXHHUD.jqfxhShowxxText("请输入要反馈的内容")
                return
            }
        }else {
            JQFXHHUD.jqfxhShowxxText("请输入要反馈的内容")
            return

        }
       
        JQFXHHUD.jqfxhShowssLoading()

        let provider = JQFXHNetworkManager.shared.provider
        if JQFXHNetworkManager.shared.partnerId == 1 {
            JQFXHNetworkManager.shared.jqfxhRequest(provider: provider, target: .jqfxhFeednnBack(contentStr: self.jqfxhDesTextggView.text)) { result in
                JQFXHHUD.jqfxhHttidden()
                switch result {
                case .success(_):
                    JQFXHHUD.jqfxhShowvvSuccess("反馈成功")
                    self.navigationController?.popViewController(animated: true)
                case .failure(let error):
                    print(error)
                }
            }
        }else {
            JQFXHNetworkManager.shared.jqfxhRequest(provider: provider, target: .jqfxhThirdFeedooBack(contentStr: self.jqfxhDesTextggView.text)) { result in
                JQFXHHUD.jqfxhHttidden()
                switch result {
                case .success(_):
                    JQFXHHUD.jqfxhShowvvSuccess("反馈成功")
                    self.navigationController?.popViewController(animated: true)
                case .failure(let error):
                    print(error)
                }
            }
        }
        
    }
    
    
    
    @objc func jqfxhDoneTapped() {
        view.endEditing(true) // 收起键盘
    }
    
    
    
    func jqfxhSetupiiUI() {
        
        self.view.addSubview(jqfxhDesffLabel)
        jqfxhDesffLabel.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(16)
            make.top.equalTo(30)
        }
        
        self.view.addSubview(jqfxhDesTextggView)
        jqfxhDesTextggView.snp.makeConstraints { make in
            make.left.equalTo(jqfxhDesffLabel)
            make.right.equalTo(self.view).offset(-16)
            make.top.equalTo(jqfxhDesffLabel.snp.bottom).offset(7)
            make.height.equalTo(100)
            
        }
        
        
        // 提交按钮
        self.view.addSubview(jqfxhSubhhBtn)
        jqfxhSubhhBtn.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(28)
            make.right.equalTo(self.view).offset(-28)
            make.bottom.equalTo(self.view).offset(-UIApplication.shared.jqfxhSafeAreaInsets.bottom-30)
            make.height.equalTo(40)
        }
       
    }
    
    func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
        if text == "\n" {
            textView.resignFirstResponder()
            
            return false
        }
        return true

    }
    

}
